

#ifndef Parser_h_included
#define Parser_h_included

#include "parserbase.h"
#include "../scanner/scanner.h"
#include "../Node.ih"

using namespace std;

#undef Parser

class Parser : public ParserBase {
public:

    /**
     * Takes a string to parse and returns a parse result calss.
     * @param a string to parse
     * @return pointer to top tree node
     */
    Node * parseThis(string inPut);

private:
    /**
     * Temporary story of string that is being parsed so we can access it later
     * if there is an error.
     */
    string toParse;
    
    Node* m_lastParse;
    
    /**
     * This enum class is taken by the makeNode method to tell what 
     * kind of node to make.
     */
    enum class nodeType {TIMES, PLUS, MINUS, DIVIDE, POWER, EQUALS, SIGN,
    FUNCTION, VARIABLE, NEGATION, NUMBER, EXPR_LIST, PERCENT, IS, OF, PARENTHESES};
    
    /**
     * Saves the top node to the parseTree member variable, so the parseThis
     * method can find it and return it.
     * @param 
     */
    void saveTree(Node*);

    /**
     * The lexer or scanner used by Parser::lex() to get the next
     * token.
     */
    Scanner m_scanner;

    /**
     * Might need to do some error handling here. 
     */
    void reset();
    void reset2();
    
    /**
     * Used by parser to make a new node.
     * @param type
     * @param lhs
     * @param rhs
     * @return 
     */
    Node* makeNode(nodeType type, Node* lhs = nullptr, Node* rhs = nullptr);
    Node* makeTwoOperatorNode(Node* newNode, Node* lhs, Node* rhs);
    Node* makeOneOperatorNode(Node* newNode, Node* lhs);
    Node* makeNodeParentheses(Node* childNode, bool leftPar, bool rightPar);
    
    
    /***********************************************************************
     * Do *not* edit the method declarations for the three methods
     * bellow this line. The implementations *may* be edited as needed.
     * Just the method signatures must stay the same so the auto generated
     * code can call them as needed.
     **********************************************************************/
private:
    void error(char const *msg);
    int lex();
    void print();

    /***********************************************************************
     * Do *not* edit the method declarations or implementations for the six
     * methods bellow this line. They are automatically generated by bisonc++
     * and implemented in the parse.cc file.
     **********************************************************************/
public:
    int parse();

private:
    void executeAction(int d_ruleNr);
    void errorRecovery();
    int lookup(bool recovery);
    void nextToken();
    void print__();
};

inline void Parser::error(char const *msg) {
    std::cerr << "Parser error: " << msg << '\n';
}

inline int Parser::lex() {
    return m_scanner.lex();
}

inline void Parser::print() {
}

#endif